package com.example.intelligentattendancesystem.service.impl;

import com.example.intelligentattendancesystem.mapping.*;
import com.example.intelligentattendancesystem.pojo.*;
import com.example.intelligentattendancesystem.service.AbsenceService;
import com.example.intelligentattendancesystem.service.LateService;
import com.example.intelligentattendancesystem.service.UserService;
import com.example.intelligentattendancesystem.service.WorkService;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author
 * @version 1.0
 * @className UserServiceImpl
 * @since 1.0
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapping userMapping;

    @Autowired
    HistoryMapping historyMapping;

    @Autowired
    AbsenceMapping absenceMapping;

    @Autowired
    LateMapping lateMapping;

    @Autowired
    LeaveMapping leaveMapping;

    @Autowired
    WorkMapping workMapping;


    @Override
    public List<User> getUserListService() {
        List<User> users = userMapping.selectAllUser();
        return users;
    }

    @Override
    public User getUserService(int userId) {
        User user = userMapping.selectUserByUserId(userId);
        return user;
    }

    @Override
    public User updateUserService(User user) {
        User oldUser = userMapping.selectUserByUserId(user.getUserId());
        user.setId(oldUser.getId());
        int i =0;
        i = userMapping.updateUser(user);
        if(i>0)//成功返回新用户
            return user;
        //失败返回旧记录
        return oldUser;
    }

    @Override
    public boolean deleteUserService(int userId) {
        int i=0;
        i += userMapping.deleteUserByUserId(userId);
        List<Absence> absences = absenceMapping.selectAbsenceByUserId(userId);
        if(absences!=null){
            i += absenceMapping.deleteUserAllAbsence(userId);
        }
        List<History> histories = historyMapping.selectHistoryByUserId(userId);
        if(histories!=null){
            i += historyMapping.deleteHistoryByUserId(userId);
        }
        List<Late> lates = lateMapping.selectLateByUserId(userId);
        if(lates!=null){
            i += lateMapping.deleteLateByUserId(userId);
        }
        List<Leave> leaves = leaveMapping.selectLeaveByUserId(userId);
        if(lates!=null){
            i += leaveMapping.deleteLeaveByUserId(userId);
        }
        List<Work> works = workMapping.selectWorkByUserId(userId);
        if (works != null) {
            i += workMapping.deleteUserAllWork(userId);
        }

        File file = new File("/usr/local/bttomcat/tomcat9/webapps/ROOT/static/"+userId);
        try {
            FileUtils.deleteDirectory(file);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        if(i>0)
            return true;
        return false;
    }

    @Override
    public List<User> getUsersService(List<Integer> userIds) {
        List<User> users = userMapping.selectAllUser();
        List<User> users2 = new ArrayList<>();
        for (Integer id :userIds) {
            for(User user:users){
                if(id==user.getUserId()){
                    users2.add(user);
                    break;
                }
            }
        }
        System.out.println(users2);
        System.out.println(users);
        System.out.println(userIds);
        return users2;
    }
}
